package com.ship.controller;

import com.ship.commens.Result;
import com.ship.dto.ShipDto;
import com.ship.dto.ShipSaveDto;
import com.ship.service.ShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/ships")
public class ShipController {

    @Resource
    private ShipService shipService;

    /*多条件查询*/
    @RequestMapping("/getShipsBySearch.do")
    public Result getShipsBySearch(@Validated ShipDto shipDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }
        return shipService.findShipsBySearch(shipDto);
    }

    //添加船舶
    @RequestMapping("/addShip.do")
    public Result addShip(@Validated ShipSaveDto shipSaveDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }

        return shipService.saveShip(shipSaveDto);
    }

    //单个删除船舶
    @RequestMapping("/cutOne.do")
    public Result cutOne(Integer id){
        if (id==null || id<1){
            return Result.DATA_FORMAT_ERROR;
        }
        return shipService.removeOne(id);
    }

    //批量删除
    @RequestMapping("/cutMany.do")
    public Result cutMany(String ids){
        if (ids==null||!(ids+",").matches("([1-9][0-9]*,)+")){
            return Result.DATA_FORMAT_ERROR;
        }
        return shipService.removeMany(ids);
    }

    //编辑船舶
    @PostMapping("/editShip.do")
    private Result editShip(@Validated ShipSaveDto shipSaveDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }
        return shipService.modifyShip(shipSaveDto);
    }

    //查询船上的船员信息
    @GetMapping("/getShipUserList.do")
    private Result getShipUserList(Long id){
        if (id==null||id<1){
            return Result.DATA_FORMAT_ERROR;
        }
        return shipService.findShipUserList(id);
    }

    //查询所有船舶
    @RequestMapping("/getAllShip.do")
    public Result getAllShip(){
        return shipService.findAllShip();
    }
}
